# -*- coding: utf-8 -*-
import xml.etree.ElementTree as etree
class oval_mapper():
    def __find_oval_data(self):
        if len(list(self.root)):
            if self.root.find(self.namespaces["xmlns"]+"definitions") != None:
                block = list(self.root.find(self.namespaces["xmlns"]+"definitions"))
                for instance in block:
                    if "id" in instance.attrib.keys():
                        #instance.tag = instance.tag.replace(self.namespaces["xmlns"],"")
                        self.definitions[instance.attrib["id"]] = instance

            if self.root.find(self.namespaces["xmlns"]+"tests") != None:
                block = list(self.root.find(self.namespaces["xmlns"]+"tests"))
                for instance in block:
                    if "id" in instance.attrib.keys():
                        #instance.tag = instance.tag.replace(self.namespaces["xmlns"],"")
                        self.tests[instance.attrib["id"]] = instance

            if self.root.find(self.namespaces["xmlns"]+"objects") != None:
                block = list(self.root.find(self.namespaces["xmlns"]+"objects"))
                for instance in block:
                    if "id" in instance.attrib.keys():
                        #instance.tag = instance.tag.replace(self.namespaces["xmlns"],"")
                        self.objects[instance.attrib["id"]] = instance

            if self.root.find(self.namespaces["xmlns"]+"states") != None:
                block = list(self.root.find(self.namespaces["xmlns"]+"states"))
                for instance in block:
                    if "id" in instance.attrib.keys():
                        #instance.tag = instance.tag.replace(self.namespaces["xmlns"],"")
                        self.states[instance.attrib["id"]] = instance

            if self.root.find(self.namespaces["xmlns"]+"variables") != None:
                block = list(self.root.find(self.namespaces["xmlns"]+"variables"))
                for instance in block:
                    if "id" in instance.attrib.keys():
                        #instance.tag = instance.tag.replace(self.namespaces["xmlns"],"")
                        self.vars[instance.attrib["id"]] = instance

    def __init__(self, filename):
        #### MAIN CONSTRUCTION
        self.tree = ""
        self.root = etree.Element("default")
        self.definitions = dict()
        self.objects = dict()
        self.tests = dict()
        self.states = dict()
        self.vars = dict()
        #### ADDITIONAL VARS
        self.namespaces = dict()
        ############### HARDCODED BASIC NAMESPACES
        self.namespaces["xmlns"] = "http://oval.mitre.org/XMLSchema/oval-definitions-5"
        self.namespaces["xmlns:oval"] = "http://oval.mitre.org/XMLSchema/oval-common-5"
        self.namespaces["xmlns:oval-def"] = "http://oval.mitre.org/XMLSchema/oval-definitions-5"
        for key in self.namespaces.keys():
            self.namespaces[key] = "{"+self.namespaces[key]+"}"
        if filename:
            file = open(filename, 'r')
            body = file.read()
            file.close()
            if body:
                self.root = etree.fromstring(body)
                self.tree = etree.ElementTree(self.root)
                self.__find_oval_data()

